import Vue from 'vue'
const state = {
  retrieveMids: {},
  retrieveMans: {}
}

const getters = {
  getRetrieveMids: state => {
    return state.retrieveMids
  },
  getRetrieveMans: state => {
    return state.retrieveMans
  }
}

const mutations = {
  muRetrieveMans (state, payload) {
    if (payload.data.isCompany) {
      Vue.set(state.retrieveMans, payload.rId, [])
      Vue.set(state.retrieveMids, payload.rId, [])
    }
    if (!payload.multiple) {
      Vue.set(state.retrieveMans, payload.rId, [])
      Vue.set(state.retrieveMids, payload.rId, [])
    }
    const mans = state.retrieveMans[payload.rId]
    const mids = state.retrieveMids[payload.rId]
    const mid = payload.data.mid
    const name = payload.data.mName || payload.data.name || payload.data.mname
    let obj = {
      mid: mid,
      name: name,
      data: payload.data
    }
    if (payload.data.dStatus) {
      obj['dStatus'] = payload.data.dStatus
    }
    if (mans) {
      let state = false
      mids.forEach(x => {
        if (x === mid) {
          state = true
        }
      })
      if (state) {
        return false
      }
      mans.push(obj)
      mids.push(mid)
    } else {
      const manArr = []
      const midArr = []
      manArr.push(obj)
      midArr.push(mid)
      Vue.set(state.retrieveMans, payload.rId, manArr)
      Vue.set(state.retrieveMids, payload.rId, midArr)
    }
  },
  sortRetrieveMans (state, payload) {
    const {newIndex, oldIndex, rId} = payload
    const newMid = state.retrieveMids[rId][oldIndex]
    state.retrieveMids[rId].splice(oldIndex, 1)
    state.retrieveMids[rId].splice(newIndex, 0, newMid)
  },
  muDelRetrieveMans (state, payload) {
    const retrieveMansArr = state.retrieveMans[payload.rId]
    const retrieveMidsArr = state.retrieveMids[payload.rId]
    retrieveMansArr.forEach((el, i) => {
      if (el.mid === payload.data) {
        retrieveMansArr.splice(i, 1)
      }
    })
    retrieveMidsArr.forEach((el, i) => {
      if (el === payload.data) {
        retrieveMidsArr.splice(i, 1)
      }
    })
    state.retrieveMans[payload.rId] = retrieveMansArr
    state.retrieveMids[payload.rId] = retrieveMidsArr
  },
  muDelAllRetrieveMans (state, payload) {
    state.retrieveMans[payload.rId] = ''
    state.retrieveMids[payload.rId] = ''
  },
  reset (state) {
    state.retrieveMids = {}
    state.retrieveMans = {}
    state.department = {}
    state.departmentsId = {}
  }
}

const actions = {
  actRetrieveMans ({ commit }, payload) {
    return new Promise((resolve, reject) => {
      commit({
        type: 'muRetrieveMans',
        data: payload.data,
        rId: payload.rId,
        multiple: payload.multiple
      })
      resolve()
    })
  },
  delRetrieveMans ({ commit }, payload) {
    return new Promise((resolve, reject) => {
      commit({
        type: 'muDelRetrieveMans',
        data: payload.data,
        rId: payload.rId,
        multiple: payload.multiple
      })
    })
  },
  setRetrieveMans ({ commit }, payload) {
    return new Promise((resolve, reject) => {
      payload.data.forEach(x => {
        if (x.dStatus) {
          x['dStatus'] = x.dStatus
        }
        commit({
          type: 'muRetrieveMans',
          data: x,
          rId: payload.rId,
          multiple: payload.multiple
        })
      })
    })
  },
  setRetrieveCompany ({ commit }, payload) {
    return new Promise((resolve) => {
      const node = {
        mid: '',
        mName: '全公司',
        isCompany: true
      }
      commit({
        type: 'muRetrieveMans',
        data: node,
        rId: payload.rId,
        multiple: payload.multiple
      })
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
